package zozopo.manual;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

import zozopo.database.BusiDatabaseManager;
import zozopo.manual.operations.ReleaseOS;
import zozopo.manual.operations.ReleaseTB;
import zozopo.operations.comm.DictionaryOS;
import zozopo.operations.comm.DictionaryTB;

import com.opensymphony.xwork2.ActionSupport;

public class TopicsAction extends ActionSupport{
	
	private int countPage = 0;
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int currentPage;
	private int rowNum = 0;
	private int pageNum = 1;
	private int nextPage = 1;
	private int backPage = 1;
	
	
	private String brand;
	private String search;
	private String lable;
	private String kind;

	List<ReleaseTB> releaseList;


	public String getKind() {
		return kind;
	}




	public void setKind(String kind) {
		this.kind = kind;
		if(kind != null){
			if(kind.equals("")){
				this.kind = null;
			}
			if(kind.equals("null")){
				this.kind = null;
			}
		}
		
	}




	public String getLable() {
		return lable;
	}




	public void setLable(String lable) {
		this.lable = lable;
		if(lable != null){
			if(lable.equals("")){
				this.lable = null;
			}
			if(lable.equals("null")){
				this.lable = null;
			}
		}
		//
	}




	public String getBrand() {
		return brand;
	}




	public void setBrand(String brand) throws UnsupportedEncodingException {
		this.brand = java.net.URLDecoder.decode(brand, "UTF-8");
		
	}




	public int getNextPage() {
		return nextPage;
	}

	


	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}




	public int getBackPage() {
		return backPage;
	}




	public void setBackPage(int backPage) {
		this.backPage = backPage;
	}


	

	
	public String execute(){
		BusiDatabaseManager databaseConnection = new BusiDatabaseManager();
		Connection conn = databaseConnection.getConnection();
		
		
		ReleaseOS releaseOS = new ReleaseOS(conn);
		if(lable != null){
			rowNum = releaseOS.getRowNumByLable(lable);
		}else if(brand != null){
			rowNum = releaseOS.getRowNumByBrand(brand);
		}else if(search != null){ 
			rowNum = releaseOS.getRowNumBySubject(search);
		}else if(kind != null){
			rowNum = releaseOS.getRowNumByKind(kind);
		}else{
			rowNum = releaseOS.getRowNum(); 
		}
		
		
		
		//beginPageNum =  (pageNum - 1) * pageSize;
		countPage = rowNum/pageSize+1;
		
		
		backPage = pageNum - 1;
		nextPage = pageNum + 1;
		
		if(nextPage > countPage){
			nextPage = countPage;
		}
		if(backPage == 0){
			backPage = 1;
		}
		
		
		if(lable != null){
			releaseList = releaseOS.saerchByLable(lable, beginPageNum, pageSize);
		}else if(brand != null){
			releaseList = releaseOS.selectRelatedBrand(brand, beginPageNum, pageSize);
		}else if(search != null){
			releaseList = releaseOS.saerchBySubject(search, beginPageNum, pageSize);
		}else if(kind != null){
			releaseList = releaseOS.saerchByKind(kind, beginPageNum, pageSize);
		}else{
			releaseList = releaseOS.selectNewRelease(beginPageNum, pageSize);
		}
		//ReleaseTB release = new ReleaseTB();
		
		
		if(releaseList == null){
			return ERROR;
		}
		
		databaseConnection.releaseConnection(conn);
		return SUCCESS;
		
	}




	public String getSearch() {
		return search;
	}




	public void setSearch(String search) throws UnsupportedEncodingException {
		this.search = java.net.URLDecoder.decode(search, "UTF-8");
		if(search != null){
			if(search.equals("")){
				this.search = null;
			}
			if(search.equals("null")){
				this.search = null;
			}
		}
	}




	public int getPageSize() {
		return pageSize;
	}




	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}




	public int getCountPage() {
		return countPage;
	}


	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}


	public int getBeginPageNum() {
		return beginPageNum;
	}


	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public int getRowNum() {
		return rowNum;
	}


	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}


	public int getPageNum() {
		return pageNum;
	}


	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}


	public List<ReleaseTB> getReleaseList() {
		return releaseList;
	}


	public void setReleaseList(List<ReleaseTB> releaseList) {
		this.releaseList = releaseList;
	}
}
